package main

import (
	"fmt"
	"math/rand"
	"time"
)

func main() {
	rand.Seed(time.Now().UnixNano())

	count := 0
	ch := make(chan bool)
	for i := 0; i < 10; i++ {
		go func(id int) {
			ch <- requestVote(id)
		}(i)
	}
	for i := 0; i < 10; i++ {
		v := <-ch
		fmt.Printf("vote: %v\n", v)
		if v {
			count += 1
		}
	}
	if count >= 5 {
		println("received 5+ votes!")
	} else {
		println("lost")
	}
}

func requestVote(id int) bool {
	time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
	res := rand.Int()%2 == 0
	fmt.Printf("requestVote from %d: %v\n", id, res)
	return res
}
